package org.dtrd.modules.food.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.food.entity.bean.CookbooksDetail;
import org.dtrd.modules.food.entity.bean.CookbooksListInfo;
import org.dtrd.modules.food.entity.request.CookbooksPageRequest;
import org.dtrd.modules.food.service.IDtrdExtEmsYwCookbooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 菜谱 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-09
 */
@Api(tags = "食物库")
@RestController
@RequestMapping("/100/16/")
public class CookbookController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwCookbooksService cookbooksService;

    @ApiOperation(value = "查询菜谱详情", response = CookbooksDetail.class)
    @GetMapping("1001624")
    @AutoLog(value="查询菜谱详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCookbooks(@RequestParam Integer id){
        CookbooksDetail info = cookbooksService.getCookbooks(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "分页查询菜谱列表", response = CookbooksListInfo.class)
    @PostMapping("1001623")
    @AutoLog(value="查询菜谱列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCookbooksList(@RequestBody CookbooksPageRequest request){
        handlePageRequest(request);
        IPage<CookbooksListInfo> page = cookbooksService.pageCookbooks(request);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改菜谱")
    @PostMapping("1001625")
    @AutoLog(value="新增或修改菜谱", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateCookbooks(@RequestBody CookbooksDetail dto){
        if (StrUtil.isBlank(dto.getCookName())
                || dto.getCookCategoryid() == null) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = cookbooksService.saveOrUpdateCookbooks(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除菜谱")
    @DeleteMapping("1001626")
    @AutoLog(value="删除菜谱", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeCookbooks(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = cookbooksService.removeCookbooks(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
